/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import translator.Translator;

/**
 * ServerThread class for handling incomming connections.
 * @author Luuk
 */
public class ServerThread implements Runnable{
    private Socket client;
    
    /**
     * Basic constructor the a ServerThread.
     * @param client, the socket from which the client connected.
     */
    public ServerThread(Socket client){
        this.client = client;
    }
    
    /**
     * The overridden run method handles the incomming connection and writes back a translated word.
     */
    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);
            String line;
            while((line = br.readLine()) != null){
                String translated = processRequest(line);
                out.println(createResponse(translated));
            }
            br.close();
            out.close();
            client.close();
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Handles all the different functions to be able to translate a word.
     * @param line, the word to be translated.
     * @return translated, the translated word.
     * @throws IOException 
     */
    public String processRequest(String line) throws IOException{
        String[] input = line.split(";");
        String langDest = getLangDest(input[0]);
        String toTrans = getToTrans(input[1]);
        
        String translated;
        if(langDest.equalsIgnoreCase("dutch")){
            translated = Translator.getTranslator().translateEngToDutch(toTrans);
        }else{
            translated = Translator.getTranslator().translateDutchToEng(toTrans);
        }
        return translated;
    }
    
    /**
     * Get the language destination of the tranlation.
     * @param input, the first line of the exchange protocol.
     * @return string of the destination language.
     */
    public String getLangDest(String input){
        Pattern pattern = Pattern.compile("translate:(.+)>(.+)");
        Matcher matcher = pattern.matcher(input.toLowerCase());
        matcher.find();
        return matcher.group(2);
    }
    
    /**
     * Get the word that has to be tranlated.
     * @param input, the second line of the exchange protocol.
     * @return string of the word that has to be translated.
     */
    public String getToTrans(String input){
        Pattern pattern = Pattern.compile("string:(.+)");
        Matcher matcher = pattern.matcher(input.toLowerCase());
        matcher.find();
        return matcher.group(1);
    }
    
    /**
     * Creates a correct response with the exchange protocol
     * @param text the translated text
     * @return a response via the exchange protocol
     */
    public String createResponse(String text){
        return "status:1;translated:"+text;
    }
    
}
